class EventsController < ApplicationController
	before_filter :login_required
	layout :set_layout
	
	def index
		params[:page_rss] ||= 1
		rss_close = params[:rss_channel] && !params[:rss_channel][:url].blank? && params[:rss_channel][:url] == "0"
		
		@user = params[:user_id] ? User.find(params[:user_id]) : current_user
		
		if params[:rss_channel] && !params[:rss_channel][:url].blank? && !rss_close
			@rss_channel = @user.rss_channels.find(params[:rss_channel][:url])
			@rss_posts = @user.rss_posts([@rss_channel], 1, 20)
		else
			@rss_posts = rss_close ? [] : @user.rss_posts(@user.rss_channels, params[:page_rss], 20)
		end
		
		@posts = @user.relative_posts(params[:page])
		@rss_channels = @user.rss_channels
	end

	def list
    @communities = current_user.communities
		@friends = current_user.pending_friends_by_me + current_user.friends
	end

	def create
		current_user.events.create!(:eventable_id => params[:eventable_id], :eventable_type => params[:eventable_type])
		render :nothing => true
	end
	
	def save_list
	 redirect_to events_path
	end

	def destroy
		event = current_user.events.find(:first, :conditions=>{:eventable_id => params[:eventable_id], :eventable_type => params[:eventable_type]})
		event.destroy
		render :nothing => true
	end

	protected
	
	def set_layout
		return "journal" if ['index'].include?(action_name)
		return 'application'
	end
end
